package com.opentd.game;

public class GameLoop implements Runnable{
	
	
	private Thread thread;
	
	/**
	 * This will call the main game loop and be responsible for stopping the program once
	 * the game is finished, as well as handling any uncaught exceptions that might be
	 * thrown from the game loop.
	 */
	public void run() {
		try{
			loop();
		}catch(Exception e){
			System.out.println("Unhandled exception: ");
			e.printStackTrace();
		}
		exit();
	}
	
	/**
	 * Will contain all of the main game loop
	 */
	private void loop(){
		while(true){
			if(Game.State.currentScreen == Game.State.GAME_SCREEN){
				Game.selectedLevel.logicLoop();
			}
			sleep(2);
		}
	}
	
	/**
	 * Terminate the current JVM
	 */
	public static void exit(){
		System.exit(0);
	}
	
	/**
	 * Pauses the current thread for the specified amount of time in milliseconds
	 * @param milis
	 */
	private void sleep(int millis){
		try{
			Thread.sleep(millis);
		}catch(Exception e){}
	}

	public void start() {
		thread = new Thread(this);
		thread.start();
	}
}
